package org.sys.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.base.vo.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.sys.services.LoginService;
import org.sys.services.UserService;
import org.sys.vo.Authentication;
import org.sys.vo.User;

@Controller
@RequestMapping("/sys")
public class LoginController {
	
	@Autowired
	private LoginService loginService;
	@Autowired
	private UserService userService;
	private static Logger log = LoggerFactory.getLogger(LoginController.class);
	
	@RequestMapping("/login")
	public ModelAndView login(String userId, String password, HttpServletRequest request){
		Authentication auth = new Authentication();
		auth.setUserId(userId);
		auth.setPassword(password);
		
		Message message = loginService.login(auth);

		log.debug("username = {}",auth.getUserId());
		log.debug("password = {}",auth.getPassword());
		log.debug("loginState = {}", message.getExecuteState());

		if (message.getExecuteState().equals("00")){
			User user = new User();
			user.setUserId(userId);
			request.getSession().setAttribute("user", userService.getUser(user));
			
			ModelAndView view = new ModelAndView("/jsp/main");
			return view;
		}
		
		Map<String,String> map = new HashMap<String,String>();
		map.put("error", "密码错误");
		ModelAndView view = new ModelAndView("/index",map);
		
		return view;
	}

}
